import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp();
// component/personnel-management/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    packList: []
  },

  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.getData();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    }
  },

  pageLifetimes: {
    show: function () {
      wx.getStorage({
        key: "back",
        success: (res) => {
          let back = res.data;
          if (back == 1) {
            this.getData();
          }
          wx.removeStorage({
            key: "back"
          });
        }
      });
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 跳转详情
    onDetail(event) {
      let id = event.target.dataset.id;
      wx.navigateTo({
        url: "/pages/management/add-pack/index?id=" + id
      });
    },
    getData() {
      wx.showLoading({
        title: "获取中..."
      });
      wx.$apis.getPack(app.globalData.userInfo.companyData.companyId).then((res) => {
        wx.stopPullDownRefresh();
        if (res.data) {
          this.setData({
						packList: res.data.map(item => {
							return {
                ...item,
                price: wx.$getYuan(item.price)
              };
						})
          });
        }
      });
    },
    onDelete(event) {
      let id = event.target.dataset.id;
      Dialog.confirm({
        message: "确定删除吗？"
      }).then(() => {
        this.deletePack(id);
      });
    },
    // 删除包装
    deletePack(id) {
      wx.showLoading({
        title: "删除中，请稍后..."
      });
      wx.$apis.deletePack(id).then((res) => {
        wx.showToast({
          title: "删除成功",
          icon: "success",
          duration: 1000
        }).then(() => {
          setTimeout(() => {
            this.getData();
          }, 1000);
        });
      });
    },
    toAddPack() {
      wx.navigateTo({
        url: "/pages/management/add-pack/index"
      });
    },
    onPullDownRefresh() {
      this.getData();
      setTimeout(() => {
        wx.stopPullDownRefresh();
      }, 1000);
    },
    onReachBottom() {
      // this.getData()
    }
  }
});
